package br.inf.solutions.apresentacoes.ssp.control.validator;

import java.util.Date;
import java.util.Map;

import br.com.vexillum.util.DateCalc;
import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.TransferUnity;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructEntity;

public class TransferUnityValidator extends BaseValidator {

	public TransferUnityValidator(Map<String, Object> mapData) {
		super(mapData);
	}
	
	@Override
	public Return validateSave() {
		Return ret = super.validateSave();
		if(ret.isValid())
			ret.concat(validateDateSubmission());
			ret.concat(existsUnity());
		return ret;
	}
	
	
	private Return existsUnity(){
		Return ret = new Return(true);
		TransferUnity transfer = (TransferUnity) entity;
		
		HierarchyStructEntity unity1 = transfer.getPerson() == null ? null : transfer.getPerson().getUnity();
		HierarchyStructEntity unity2 = transfer.getUnitDestiny();
		
		if(unity1 != null && unity2 != null){
			if(unity2.equals(unity1)){
				ret.concat(creatReturn("unity", getValidationMessage(null, "unitydestiny", false)));
			}
		}
		
		
		return ret;
	}
	
	
	public Return validateDateSubmission(){
		Return ret = new Return(true);
		TransferUnity transfer = (TransferUnity) entity;
		Date date1 = DateCalc.incrementDateInDays(transfer.getDateSubmission(), 1);
		Date date2 = new Date();
		if(date2 != null){
			if(date1.before(date2)){
				ret.concat(creatReturn("dateSubmission", getValidationMessage(null, "datesubmission", false)));
			}
		}
		return ret;
	}

}
